package org.example.managere.controller.feibiao;

import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmArchivesRelationDto;
import org.example.managere.service.feibiao.AmArchivesRelationService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电机标准关联控制器
 */
@RestController
@RequestMapping("/api/feibiao/relation")
@RequiredArgsConstructor
public class AmArchivesRelationController {

    private final AmArchivesRelationService relationService;

    /**
     * 获取所有标准关联（分页）
     */
    @GetMapping
    public ResponseEntity<Page<AmArchivesRelationDto>> getAllRelations(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String sort) {
        Pageable pageable;
        if (sort != null && !sort.isEmpty()) {
            String[] sortParams = sort.split(",");
            String sortField = sortParams[0];
            Sort.Direction direction = (sortParams.length > 1 && sortParams[1].equalsIgnoreCase("asc"))
                    ? Sort.Direction.ASC : Sort.Direction.DESC;
            pageable = PageRequest.of(page, size, Sort.by(direction, sortField));
        } else {
            pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "relationDate"));
        }
        return ResponseEntity.ok(relationService.findAll(pageable));
    }

    /**
     * 根据ID获取标准关联
     */
    @GetMapping("/{relationId}")
    public ResponseEntity<AmArchivesRelationDto> getRelationById(@PathVariable Integer relationId) {
        return relationService.findById(relationId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 创建标准关联
     */
    @PostMapping
    public ResponseEntity<AmArchivesRelationDto> createRelation(@RequestBody AmArchivesRelationDto relationDto) {
        return new ResponseEntity<>(relationService.save(relationDto), HttpStatus.CREATED);
    }

    /**
     * 更新标准关联
     */
    @PutMapping("/{relationId}")
    public ResponseEntity<AmArchivesRelationDto> updateRelation(
            @PathVariable Integer relationId,
            @RequestBody AmArchivesRelationDto relationDto) {
        if (relationDto.getRelationId() != null && !relationId.equals(relationDto.getRelationId())) {
            return ResponseEntity.badRequest().build();
        }
        relationDto.setRelationId(relationId);
        return ResponseEntity.ok(relationService.update(relationDto));
    }

    /**
     * 删除标准关联
     */
    @DeleteMapping("/{relationId}")
    public ResponseEntity<Void> deleteRelation(@PathVariable Integer relationId) {
        relationService.deleteById(relationId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据电机ID查询标准关联
     */
    @GetMapping("/by-motor")
    public ResponseEntity<List<AmArchivesRelationDto>> findByMotorId(@RequestParam String motorId) {
        return ResponseEntity.ok(relationService.findByMotorId(motorId));
    }

    /**
     * 根据标准编号查询标准关联
     */
    @GetMapping("/by-standard")
    public ResponseEntity<List<AmArchivesRelationDto>> findByStandardCode(@RequestParam String standardCode) {
        return ResponseEntity.ok(relationService.findByStandardCode(standardCode));
    }

    /**
     * 根据合规状态查询标准关联
     */
    @GetMapping("/by-compliance")
    public ResponseEntity<List<AmArchivesRelationDto>> findByComplianceStatus(@RequestParam String complianceStatus) {
        return ResponseEntity.ok(relationService.findByComplianceStatus(complianceStatus));
    }

    /**
     * 根据标准代码查询关联
     *
     * @param standardCode 标准代码
     * @return 标准关联列表
     */
    @GetMapping("/code/{standardCode}")
    public ResponseEntity<List<AmArchivesRelationDto>> getRelationsByStandardCode(
            @PathVariable String standardCode) {
        List<AmArchivesRelationDto> relations = relationService.findByStandardCode(standardCode);
        return ResponseEntity.ok(relations);
    }

    /**
     * 根据标准代码分页查询关联
     *
     * @param standardCode 标准代码
     * @param page 页码
     * @param size 每页大小
     * @return 标准关联分页结果
     */
    @GetMapping("/code/{standardCode}/page")
    public ResponseEntity<Page<AmArchivesRelationDto>> getRelationsByStandardCodeWithPagination(
            @PathVariable String standardCode,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<AmArchivesRelationDto> relations = relationService.findByStandardCode(standardCode, pageable);
        return ResponseEntity.ok(relations);
    }
} 